package com.jqkb.adapters;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jqkb.R;
import com.jqkb.pages.BackwardDetailActivity;
import com.jqkb.service.bean.Action;
import com.jqkb.utils.Constant;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by alimjantursun on 18/5/29.
 */
public class PendingBackwardAdapter extends RecyclerView.Adapter {
    private List<Action> data;
    private final Context context;

    public PendingBackwardAdapter(Context context) {
        this.context = context;
        data = new ArrayList();
    }


    public void setData(List<Action> list) {
        this.data.clear();
        this.data.addAll(list);
        notifyDataSetChanged();
    }

    public void addData(List<Action> list) {
        this.data.addAll(list);
        notifyDataSetChanged();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.forward_list_item, parent, false);
        return new ClassHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {


        if (holder instanceof ClassHolder) {
            Action action = data.get(position);
            ((ClassHolder) holder).dateTv.setText(action.getCreatedDate());
            ((ClassHolder) holder).titleTv.setText(action.getTitle());
            ((ClassHolder) holder).senderTv.setText(action.getCreatedUser().getName());

            if (action.getType().equals("一般")) {


                holder.itemView.setBackgroundColor(Color.WHITE);

            } else if (action.getType().equals("紧急")) {
                holder.itemView.setBackgroundColor(Color.parseColor("#f0ad4e"));
            } else if (action.getType().equals("危急")) {
                holder.itemView.setBackgroundColor(Color.parseColor("#d9534f"));
            }
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, BackwardDetailActivity.class);
                intent.putExtra(Constant.ACTION_ID, data.get(position).getId());
                context.startActivity(intent);
            }
        });

    }

    public class ClassHolder extends RecyclerView.ViewHolder {


        TextView dateTv;
        TextView titleTv;
        TextView senderTv;

        public ClassHolder(View itemView) {
            super(itemView);
            dateTv = (TextView) itemView.findViewById(R.id.dateTv);
            titleTv = (TextView) itemView.findViewById(R.id.titleTv);
            senderTv = (TextView) itemView.findViewById(R.id.senderTv);

        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
}
